package com.akai.service.impl;

import com.akai.mapper.UserMapper;
import com.akai.pojo.User;
import com.akai.service.UserService;
import com.akai.utils.Md5Util;
import com.akai.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public void update(String avatarUrl, Integer id) {
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
     public User findByUsername(String username) {
        User u = userMapper.findByUsername(username);
         return u;
     }

     @Override
     public void register(String username, String password) {
    //加密
        String md5String =  Md5Util.getMD5String(password);
    //添加
    userMapper.add(username,md5String);
     }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
         userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }
}
